package com.my.chapter10;

public class practice4 {
    public static void main(String[] args) {
        Sequence4.main(args);
    }
}

interface Selector{
    boolean end();
    Object current();
    void next();
}

class Sequence4{
    private Object[] items;
    private int next = 0;
    public Sequence4(int size){
        items = new Object[size];
    }
    public void add(Object x){
        if(next<items.length){
            items[next++] = x;
        }
    }
    private class SequenceSelector implements Selector{
        private int i = 0;

        @Override
        public boolean end() {
            return i==items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if(i<items.length){
                i++;
            }
        }

        public Sequence4 outer(){
            return Sequence4.this;
        }
    }

    public SequenceSelector selector(){
        return new SequenceSelector();
    }

    public static void main(String[] args) {
        Sequence4 sequence = new Sequence4(10);
        SequenceSelector selector = sequence.selector();
        for(int i=0;i<10;i++){
            selector.outer().add(Integer.toString(i));
        }

        while(!selector.end()){
            System.out.print(selector.current() + " ");
            selector.next();
        }
    }
}

